using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace FluentSilverlight.Expressions.Panels
{
    public abstract class PanelExpression<THIS, TModel, VIEW> : FrameworkElementExpression<THIS, TModel, VIEW>
        where THIS : PanelExpression<THIS, TModel, VIEW>
        where TModel : class
        where VIEW : Panel
    {
        private Brush background;
        private readonly List<UIElement> children = new List<UIElement>();

        protected PanelExpression()
        {
        }

        protected PanelExpression(string name) : base(name)
        {
        }

        public THIS Background(Color color)
        {
            background = new SolidColorBrush(color);
            return thisInstance();
        }

        public THIS AddChild(IOpinionatedControl child)
        {
            var uiElement = child as UIElement;
            if(uiElement == null) throw new ArgumentException("Cannot add a non UIElement to a Panel");

            return AddChild(uiElement);
        }

        public THIS AddChild(UIElement child)
        {
            children.Add(child);
            return thisInstance();
        }

        protected override void BuildUp(VIEW panel)
        {
            foreach (var child in children)
                panel.Children.Add(child);

            base.BuildUp(panel);

            panel.Background = background;
        }
    }
}